package com.ruoyi.utils;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.util.List;

/**
 * 类作用：根据坐标点画检测框
 *
 * @author Mr-Le
 */
@Slf4j
public class ImgDrawCircleTool {

    /**
     * 褐飞虱
     */
    public static final String HFS = "hfs";

    /**
     * 白背飞虱
     */
    public static final String BBFS = "bbfs";

    /**
     * 大螟
     */
//    public static final String A21 = "a21";

    /**
     * 稻纵卷叶螟
     */
//    public static final String A12 = "a12";

    /**
     * 根据坐标点、标签集合画 检测框
     *
     * @param list      坐标点集合
     * @param tag       标签集合
     * @param inImgPath 输入图片路径
     * @return 返回结果图路径
     */
    public String doDrawRectangle(List<int[]> list, List<String> tag, String inImgPath) {
        String resPath = null;
        String s = StringUtils.substringAfter(inImgPath, "profile");

        File file = new File(inImgPath);

        if (list.isEmpty()) {
            return null;
        }
        if (file.exists()) {
            log.info("file exists!");
            // 原图加上 result 标签
            resPath = addResult(inImgPath);
            try {
                BufferedImage image = ImageIO.read(new File(inImgPath));
                Graphics graphics = image.getGraphics();
                Graphics2D g = (Graphics2D) graphics;
                g.setStroke(new BasicStroke(7.0f));

                if (list.size() == tag.size()) {
                    for (int i = 0; i < list.size(); i++) {
                        Color newColor = judgeColorByTag(tag.get(i));
                        g.setColor(newColor);
                        // 检测框 坐标点 转换 x1,y1;x2,y2----------> x,y,w,h
                        g.drawRect(list.get(i)[0], list.get(i)[1], list.get(i)[2] - list.get(i)[0], list.get(i)[3] - list.get(i)[1]);
                    }
                    FileOutputStream fileOutputStream = new FileOutputStream(resPath);
                    ImageIO.write(image, "jpeg", fileOutputStream);
                } else {
                    log.info("tag和corner对应出错，检查算法端!");
                    return null;
                }
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
        } else {
            log.info("文件不存在!");
            return null;
        }
        return resPath;
    }

    /**
     * 根据tag判断检测框颜色
     *
     * @param tag 标签
     * @return 返回颜色
     */
    public static Color judgeColorByTag(String tag) {
        switch (tag) {
            case HFS:
                return Color.red;
            case BBFS:
                return Color.GREEN;
            case "a1":
                return Color.YELLOW;
            case "a2":
                return Color.LIGHT_GRAY;
            case "a3":
                return Color.GRAY;
            case "a4":
                return Color.ORANGE;
            case "a10":
                return Color.MAGENTA;
            case "a12":
                return Color.PINK;
            case "a21":
                return Color.BLUE;
            case "a102":
                return Color.CYAN;
            case "a122":
                return new Color(112, 128, 105);
            case "a22":
                return new Color(128, 128, 105);
            case "a41":
                return new Color(255, 153, 18);
            case "b13":
                return new Color(255, 227, 132);
            case "b28":
                return new Color(176, 224, 230);
            case "c21":
                return new Color(65, 105, 225);
            case "e12":
                return new Color(106, 90, 205);
            case "e40":
                return new Color(135, 206, 235);
            case "e90":
                return new Color(250, 235, 215);
            case "e120":
                return new Color(255, 235, 205);
            case "g21":
                return new Color(227, 168, 105);
            default:
                return new Color(255, 99, 71);
        }
    }

    /**
     * 添加标签：_Result 标签
     *
     * @param srcPath 字符串路径
     * @return 返回加标签之后的字符串
     */
    private static String addResult(String srcPath) {
        String res = "";
        String[] str = new String[2];
        /* \\. 转义字符，相当于 . ,分隔出.前后的内容 */
        str = srcPath.split("\\.");
        res = str[0] + "__Result." + str[1];
        return res;
    }

//    /**
//     * 根据坐标点集合画 检测框
//     * @param list 坐标点集合
//     * @param inImgPath 输入图片路径
//     * @return 返回结果图路径
//     */
//    public String doDrawRectangle(List<int[]> list, String inImgPath) {
//        String resPath = null;
//        String s = StringUtils.substringAfter(inImgPath, "profile");
//
//        File file = new File(inImgPath);
//
//        if (list.isEmpty()) {
//            return null;
//        }
//        if (file.exists()) {
//            System.out.println("file exists!");
//            // 原图加上 result 标签
//            resPath = addResult(inImgPath);
//            try {
//                BufferedImage image = ImageIO.read(new File(inImgPath));
//                Graphics graphics = image.getGraphics();
//                Graphics2D g = (Graphics2D) graphics;
//                g.setColor(Color.red);
//                g.setStroke(new BasicStroke(7.0f));
//                for (int[] ints : list) {
//                    // 检测框 坐标点 转换 x1,y1;x2,y2----------> x,y,w,h
//                    g.drawRect(ints[0], ints[1], ints[2] - ints[0], ints[3] - ints[1]);
//                }
//                FileOutputStream fileOutputStream = new FileOutputStream(resPath);
//                ImageIO.write(image, "jpeg", fileOutputStream);
//            } catch (Exception e) {
//                System.out.println(e.getMessage());
//            }
//        } else {
//            System.out.println("文件不存在");
//            return null;
//        }
//        return resPath;
//    }


}
